#!/bin/bash

DIR="$( cd "$( dirname "$0"  )" && pwd  )"
cd $DIR

monitor_db_host="10.63.0.233"
monitor_db_port=3306
monitor_db_user="slow_log"
monitor_db_password="pt-query-digest"
monitor_db_database="sqlflow"

#实例慢日志位置
slowquery_file="/data2/mysql_slow/slow.log"
hostname="prd_ph_db02:3306"

if [ -s sqlaudit_slow_query_last_time ]; then
 last_analysis_time=`cat sqlaudit_slow_query_last_time`
else
 last_analysis_time='2000-01-01 00:00:00'
fi

pt-query-digest \
--user=$monitor_db_user --password=$monitor_db_password --port=$monitor_db_port \
--review h=$monitor_db_host,D=$monitor_db_database,t=mysql_slow_query_review  \
--history h=$monitor_db_host,D=$monitor_db_database,t=mysql_slow_query_review_history  \
--no-report --limit=100% --charset=utf8 \
--since "$last_analysis_time" \
--filter="\$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$hostname\"  and \$event->{client}=\$event->{ip} " \
$slowquery_file > /tmp/analysis_slow_query.log

if [ $? -ne 0 ]; then
    echo "pt-query-digest faile" >>/tmp/analysis_slow_query.log
else
    echo `date +"%Y-%m-%d %H:%M:%S"` >sqlaudit_slow_query_last_time
fi